// 关闭所有进程
const APP = require('../APP');
class index {
    androidBot = null;
    constructor(client) {
        this.GameClient = client;
        this.androidBot = this.GameClient.androidBot;
    }
    async run() {
        await this.androidBot.home()
        await APP.sleep()
        let recents = await this.androidBot.recents();
        if (!recents) {
            console.error('最近任务', recents);
            return false;
        }
        await APP.sleep()
        // left:24 top:131 right:516 bottom:815
        // 从上往下滑动，使用矩形中心点
        await APP.swipeRect_t_b(this.androidBot, { left: 24, top: 131, right: 516, bottom: 815 }, 300); // 300ms的滑动时间

        let rect = await this.androidBot.getElementRect('com.android.systemui[1]/com.android.systemui:id=button');
        console.log('元素位置', rect); // rect格式为 {left: number, top: number, right: number, bottom: number}
        if (!rect || isNaN(rect.left)) {
            console.log('没有清除任务按钮');
            await this.androidBot.home()
            await APP.sleep(5000)
            return true;
        }
        let jude = await APP.clickRect(this.androidBot, rect)
        if (!jude) {
            console.log('点击失败');
            return false;
        }
        //home page com.android.launcher3/com.android.launcher3:id=workspace
        let info = await this.androidBot.getElementRect('com.android.launcher3/com.android.launcher3:id=workspace');
        if (!info) {
            console.error('home page', info);
            return false;
        }
        await this.androidBot.home()
        await APP.sleep()
        await this.androidBot.home()
        await APP.sleep()
        await this.androidBot.home()
        await APP.sleep()
        return true;
    }

}

module.exports = index;